package main

import (
	databases "gostep/databases/redis"
	"strconv"
)

var (
	postCountKeys="posts:count"
)

func CreatePost(p *Post)error{
	id,err:=databases.Count(postCountKeys)
	if err!=nil{

	}
	var postMap=map[string]interface{}{
		"PostId":strconv.Itoa(id+1),
		"AuthorId":p.AuthorId,
		"Content":p.Content,
		"UpdateAt":p.UpdateAt,
	}
	for i,v:=range postMap{
		err:=databases.HSet("posts:"+strconv.Itoa(id+1),i,v)
		if err!=nil{
			return err
		}
	}
	// 作者所有文章集合
	databases.DefaultRedisConn.Do("SADD","author:"+p.AuthorId,strconv.Itoa(id+1))
	// 添加到tag关系表
	databases.DefaultRedisConn.Do("SADD","post:"+strconv.Itoa(id+1)+":tags",p.Tag)
	for _,v:=range p.Tag{
		databases.DefaultRedisConn.Do("SADD","tag:"+v+":posts",strconv.Itoa(id+1))
	}
	return nil
}

func SAddPosts(){

}